
// 🆘🆘🆘公共excel导入组件

<template>
  <!-- 公共导入组件 -->
  <upload-excel :on-success="success" />
</template>

<script>
import { importEmployee } from '@/api/employees'
export default {
  name: '',
  components: {},
  props: [],
  data() {
    return {}
  },
  computed: {},
  methods: {
    // excel导入过后的回调
    async success({ header, results }) {
      console.log(header, results)
      // header为表头，中文
      // results为数组对象，每一条数据
      // 如果是导入员工，表格里面中文需要转化成对应要求的英文
      const userRelations = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber'
      }
      // 方式1-标准写法
      // const list = []
      // results.forEach(o => {
      //   var userInfo = {}
      //   for (const key in o) {
      //     const newKey = userRelations[key]
      //     const value = o[key]
      //     //   console.log('newKey', newKey, 'value', value)
      //     userInfo[newKey] = value
      //   }
      //   list.push(userInfo)
      // })
      // console.log(list)

      // 方式2.1-用Object.keys代替for in
      /*
      const list = []
      results.forEach(o => {
        var userInfo = {}
        // ['手机号', '姓名',...]
        Object.keys(o).forEach(key => {
          const newKey = userRelations[key]
          const value = o[key]
          //   console.log('newKey', newKey, 'value', value)
          userInfo[newKey] = value
        })
        list.push(userInfo)
      })
      console.log(list)
      */
      //  方式2.2-map
      /*
      const list = results.map(o => {
        var userInfo = {}
        // ['手机号', '姓名',...]
        Object.keys(o).forEach(key => {
          const newKey = userRelations[key]
          const value = o[key]
          //   console.log('newKey', newKey, 'value', value)
          userInfo[newKey] = value
        })
        return userInfo
      })
      console.log(list)
      */
      /*       const arr = []
      results.forEach(item => {
        const userInfo = {}
        Object.keys(item).forEach(key => {
          userInfo[userRelations[key]] = item[key]
        })
        arr.push(userInfo)
      })
      await importEmployee(arr) // 调用导入接口
      console.log(arr)
      this.$router.back() */

      results.forEach(e => {
        for (const key in e) {
          e[userRelations[key]] = e[key]
          if (key === '入职日期' || key === '转正日期') {
            e[userRelations[key]] = new Date(this.formatDate(e[key], '/'))
          }
          delete e[key]
        }
      })
      console.log(results)
      // 发请求
      await importEmployee(results)
      // 提示成功！
      this.$message.success('操作成功')
      // 返回上一页
      this.$router.back()
    },
    // 格式化日期函数
    formatDate(numb, format) {
      const time = new Date((numb - 1) * 24 * 3600000 + 1)
      time.setYear(time.getFullYear() - 70)
      const year = time.getFullYear() + ''
      const month = time.getMonth() + 1 + ''
      const date = time.getDate() - 1 + ''
      if (format && format.length === 1) {
        return year + format + month + format + date
      }
      return year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date)
    }
  }
}
</script>

<style lang='less' scoped>
</style>
